package com.ks.api.controller;

import com.ks.api.common.Result;
import com.ks.api.entity.UserBinding;
import com.ks.api.service.UserBindingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private UserBindingService userBindingService;

    @PostMapping("/login")
    public Result login(@RequestBody UserBinding userBinding, HttpSession session) {
        UserBinding user = userBindingService.login(userBinding.getUsername(), userBinding.getPassword());
        if (user != null) {
            session.setAttribute("user", user);
            return Result.success("登录成功");
        }
        return Result.error("用户名或密码错误");
    }

    @GetMapping("/check")
    public Result checkLogin(HttpSession session) {
        UserBinding user = (UserBinding) session.getAttribute("user");
        if (user != null) {
            return Result.success("已登录");
        }
        return Result.error("未登录");
    }

    @PostMapping("/logout")
    public Result logout(HttpSession session) {
        session.removeAttribute("user");
        return Result.success("注销成功");
    }
} 